#include <QMouseEvent>
#include <QPoint>

#include "qwgraphicsview.h"
#include "../mapsize/mapsize.h"

#ifndef SCALE_MIN
#define SCALE_MIN (12) //测试发现的倍率
#endif

#ifndef SCALE_MUL
#define SCALE_MUL (1.1) //扩大倍数
#endif


QWGraphicsView::QWGraphicsView(QWidget *parent):QGraphicsView(parent)
{
    m_bActive = false;
}

void QWGraphicsView::mouseMoveEvent(QMouseEvent *event)
{
    QPoint point=event->pos(); //QGraphicsView的坐标
    emit mouseMovePoint(point); //释放信号
    QGraphicsView::mouseMoveEvent(event);
}

void QWGraphicsView::mousePressEvent(QMouseEvent *event)
{
    if(event->button()==Qt::LeftButton)
    {
        QPointF pointf = mapToScene(event->pos()); //QGraphicsView坐标转为QGraphicsScene坐标
        emit mouseClicked(pointf);//释放信号
    }

    if(!m_bActive)
    {
        QGraphicsView::mousePressEvent(event);
    }
    else
    {
        m_bActive = false;
    }
}

void QWGraphicsView::mouseReleaseEvent(QMouseEvent *event)
{
    if(event->button()==Qt::LeftButton)
    {
        QPoint point=event->pos(); //QGraphicsView的坐标
        emit mouseReleasePoint(point);//释放信号
    }

    if(!m_bActive)
    {
        QGraphicsView::mouseReleaseEvent(event);
    }
    else
    {
        m_bActive = false;
    }
}

void QWGraphicsView::mouseDoubleClickEvent(QMouseEvent *event)
{
    if(event->button()==Qt::LeftButton)
    {
        QPoint point=event->pos(); //QGraphicsView的坐标
        emit mouseDoubleClick(point);//释放信号
    }

    QGraphicsView::mouseDoubleClickEvent(event);
}

void QWGraphicsView::keyPressEvent(QKeyEvent *event)
{
    emit keyPress(event);
    QGraphicsView::keyPressEvent(event);
}

void QWGraphicsView::wheelEvent(QWheelEvent *event)
{
    if(event->delta() > 0)
    {
        // 向上滚动，放大;
        this->scale(SCALE_MUL, SCALE_MUL);
    }
    else
    {
        // 向下滚动，缩小;
        if(transform().m11() >= __MAPFILESIZE->getMapScale() / SCALE_MIN)
        {
            this->scale(1 / SCALE_MUL, 1 / SCALE_MUL);
        }
    }

    this->setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
    this->setResizeAnchor(QGraphicsView::AnchorUnderMouse);
}

void QWGraphicsView::setBActive(bool bActive)
{
    m_bActive = bActive;
}
